<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PermissaoMapper
 *
 * @author Home
 */
class PermissaoMapper {
    
    function __construct(){
        
    }
    public static function getModulos(){
       
        TTransaction::open('my_config');
            
         if ($conn = TTransaction::get()){
               
               $sql = "SELECT nome_modulo FROM modulos";
         
               $sth = $conn->prepare($sql);
               $sth->execute();

               $result = $sth->fetchALL(PDO::FETCH_COLUMN, 0);
               return $result;
               TTransaction::close();

         }else{
                echo 'Sem conexão com banco!';
         }
        
    }
    
    public static function getPermissoes(){
       
        TTransaction::open('my_config');
            
         if ($conn = TTransaction::get()){
                
               $sql = "SELECT nome_permissao FROM permissoes";
         
               $sth = $conn->prepare($sql);
               $sth->execute();

               $result = $sth->fetchALL(PDO::FETCH_COLUMN, 0);
               return $result;
               TTransaction::close();

         }else{
                echo 'Sem conexão com banco!';
         }
        
    }
    
    public static function getModulo_Permissoes($modulos){
       
        TTransaction::open('my_config');
            
         if ($conn = TTransaction::get()){
               
               $sql = "SELECT p.nome_permissao,mp.id_modulo_permissao FROM permissoes p
                      INNER JOIN modulos_permissoes mp ON (p.id_permissao = mp.permissao_id)
                      INNER JOIN modulos m ON (m.id_modulo = mp.modulo_id)
                      WHERE (m.nome_modulo like ?)";
               $sth = $conn->prepare($sql);
               
               foreach ($modulos as $modulo){
                  $sth->execute(array($modulo));
                  $tec = $sth->fetchALL(PDO::FETCH_NUM);
                  foreach ($tec as $t)
                      $result[$modulo][$t[1]]=$t[0]; 
               }
              
               return $result;

               TTransaction::close();

         }else{
                echo 'Sem conexão com banco!';
         }
        
    }
    
    public static function getPermissoesById($id){
       
        TTransaction::open('my_config');
            
         if ($conn = TTransaction::get()){
               
               $sql = "SELECT mpu.modulo_permissao_id, mp.modulo_id FROM modulos_permissoes_usuarios mpu
                       INNER JOIN modulos_permissoes mp ON
                       (mp.id_modulo_permissao = mpu.modulo_permissao_id) 
                       WHERE usuario_id = ?
                       order by mp.modulo_id";
         
               $sth = $conn->prepare($sql);
               $sth->execute(array($id));
            
               $result = $sth->fetchALL(PDO::FETCH_NUM);
               
               return $result;
               TTransaction::close();

         }else{
                echo 'Sem conexão com banco!';
         }
        
    }
    
     public static function InsertPermissoes($id, $insert, $delete){
       
        TTransaction::open('my_config');
             
         if ($conn = TTransaction::get()){
               
               $sql = "DELETE FROM modulos_permissoes_usuarios
                       WHERE usuario_id = ? AND modulo_permissao_id =?";
 
               $sth = $conn->prepare($sql);
               foreach ($delete as $del){
               //$sth->execute(array($id,implode(',',$delete))); 
                $sth->execute(array($id,$del));
               }
               if ($insert){
                    $sql = "INSERT INTO modulos_permissoes_usuarios (usuario_id, modulo_permissao_id)
                            VALUES (?,?)";

                    $sth = $conn->prepare($sql);
                    foreach ($insert as $in){
                        $sth->execute(array($id,$in));
                    }
               }
          
               TTransaction::close();
         }else{
                echo 'Sem conexão com banco!';
         }
        
    }
   
}

?>
